For on-page attribution, only show last ($wgMaxCredits - 1) contributors. No
[lhc/web/wiklou.git] / includes / Article.php
1 <?php
2 # Class representing a Wikipedia article and history.
3 # See design.doc for an overview.
4
5 # Note: edit user interface and cache support functions have been
6 # moved to separate EditPage and CacheManager classes.
7
8 include_once( "CacheManager.php" );
9
10 class Article {
11 /* private */ var $mContent, $mContentLoaded;
12 /* private */ var $mUser, $mTimestamp, $mUserText;
13 /* private */ var $mCounter, $mComment, $mCountAdjustment;
14 /* private */ var $mMinorEdit, $mRedirectedFrom;
15 /* private */ var $mTouched, $mFileCache, $mTitle;
16 /* private */ var $mId, $mTable;
17
18 function Article( &$title ) {
19 $this->mTitle =& $title;
20 $this->clear();
21 }
22
23 /* private */ function clear()
24 {
25 $this->mContentLoaded = false;
26 $this->mCurID = $this->mUser = $this->mCounter = -1; # Not loaded
27 $this->mRedirectedFrom = $this->mUserText =
28 $this->mTimestamp = $this->mComment = $this->mFileCache = "";
29 $this->mCountAdjustment = 0;
30 $this->mTouched = "19700101000000";
31 }
32
33 /* static */ function getRevisionText( $row, $prefix = "old_" ) {
34 # Deal with optional compression of archived pages.
35 # This can be done periodically via maintenance/compressOld.php, and
36 # as pages are saved if $wgCompressRevisions is set.
37 $text = $prefix . "text";
38 $flags = $prefix . "flags";
39 if( isset( $row->$flags ) && (false !== strpos( $row->$flags, "gzip" ) ) ) {
40 return gzinflate( $row->$text );
41 }
42 if( isset( $row->$text ) ) {
43 return $row->$text;
44 }
45 return false;
46 }
47
48 /* static */ function compressRevisionText( &$text ) {
49 global $wgCompressRevisions;
50 if( !$wgCompressRevisions ) {
51 return "";
52 }
53 if( !function_exists( "gzdeflate" ) ) {
54 wfDebug( "Article::compressRevisionText() -- no zlib support, not compressing\n" );
55 return "";
56 }
57 $text = gzdeflate( $text );
58 return "gzip";
59 }
60
61 # Note that getContent/loadContent may follow redirects if
62 # not told otherwise, and so may cause a change to mTitle.
63
64 # Return the text of this revision
65 function getContent( $noredir = false )
66 {
67 global $wgRequest;
68
69 # Get variables from query string :P
70 $action = $wgRequest->getText( 'action', 'view' );
71 $section = $wgRequest->getText( 'section' );
72
73 $fname = "Article::getContent";
74 wfProfileIn( $fname );
75
76 if ( 0 == $this->getID() ) {
77 if ( "edit" == $action ) {
78 wfProfileOut( $fname );
79 return ""; # was "newarticletext", now moved above the box)
80 }
81 wfProfileOut( $fname );
82 return wfMsg( "noarticletext" );
83 } else {
84 $this->loadContent( $noredir );
85
86 if(
87 # check if we're displaying a [[User talk:x.x.x.x]] anonymous talk page
88 ( $this->mTitle->getNamespace() == Namespace::getTalk( Namespace::getUser()) ) &&
89 preg_match("/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/",$this->mTitle->getText()) &&
90 $action=="view"
91 )
92 {
93 wfProfileOut( $fname );
94 return $this->mContent . "\n" .wfMsg("anontalkpagetext"); }
95 else {
96 if($action=="edit") {
97 if($section!="") {
98 if($section=="new") {
99 wfProfileOut( $fname );
100 return "";
101 }
102
103 # strip NOWIKI etc. to avoid confusion (true-parameter causes HTML
104 # comments to be stripped as well)
105 $striparray=array();
106 $parser=new Parser();
107 $parser->mOutputType=OT_WIKI;
108 $striptext=$parser->strip($this->mContent, $striparray, true);
109
110 # now that we can be sure that no pseudo-sections are in the source,
111 # split it up by section
112 $secs =
113 preg_split(
114 "/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
115 $striptext, -1,
116 PREG_SPLIT_DELIM_CAPTURE);
117
118 if($section==0) {
119 $rv=$secs[0];
120 } else {
121 $rv=$secs[$section*2-1] . $secs[$section*2];
122 }
123
124 # reinsert stripped tags
125 $rv=$parser->unstrip($rv,$striparray);
126 $rv=trim($rv);
127 wfProfileOut( $fname );
128 return $rv;
129 }
130 }
131 wfProfileOut( $fname );
132 return $this->mContent;
133 }
134 }
135 }
136
137 # Load the revision (including cur_text) into this object
138 function loadContent( $noredir = false )
139 {
140 global $wgOut, $wgMwRedir, $wgRequest;
141
142 # Query variables :P
143 $oldid = $wgRequest->getVal( 'oldid' );
144 $redirect = $wgRequest->getVal( 'redirect' );
145
146 if ( $this->mContentLoaded ) return;
147 $fname = "Article::loadContent";
148
149 # Pre-fill content with error message so that if something
150 # fails we'll have something telling us what we intended.
151
152 $t = $this->mTitle->getPrefixedText();
153 if ( isset( $oldid ) ) {
154 $oldid = IntVal( $oldid );
155 $t .= ",oldid={$oldid}";
156 }
157 if ( isset( $redirect ) ) {
158 $redirect = ($redirect == "no") ? "no" : "yes";
159 $t .= ",redirect={$redirect}";
160 }
161 $this->mContent = wfMsg( "missingarticle", $t );
162
163 if ( ! $oldid ) { # Retrieve current version
164 $id = $this->getID();
165 if ( 0 == $id ) return;
166
167 $sql = "SELECT " .
168 "cur_text,cur_timestamp,cur_user,cur_counter,cur_restrictions,cur_touched " .
169 "FROM cur WHERE cur_id={$id}";
170 wfDebug( "$sql\n" );
171 $res = wfQuery( $sql, DB_READ, $fname );
172 if ( 0 == wfNumRows( $res ) ) {
173 return;
174 }
175
176 $s = wfFetchObject( $res );
177 # If we got a redirect, follow it (unless we've been told
178 # not to by either the function parameter or the query
179 if ( ( "no" != $redirect ) && ( false == $noredir ) &&
180 ( $wgMwRedir->matchStart( $s->cur_text ) ) ) {
181 if ( preg_match( "/\\[\\[([^\\]\\|]+)[\\]\\|]/",
182 $s->cur_text, $m ) ) {
183 $rt = Title::newFromText( $m[1] );
184 if( $rt ) {
185 # Gotta hand redirects to special pages differently:
186 # Fill the HTTP response "Location" header and ignore
187 # the rest of the page we're on.
188
189 if ( $rt->getInterwiki() != "" ) {
190 $wgOut->redirect( $rt->getFullURL() ) ;
191 return;
192 }
193 if ( $rt->getNamespace() == Namespace::getSpecial() ) {
194 $wgOut->redirect( $rt->getFullURL() );
195 return;
196 }
197 $rid = $rt->getArticleID();
198 if ( 0 != $rid ) {
199 $sql = "SELECT cur_text,cur_timestamp,cur_user," .
200 "cur_counter,cur_restrictions,cur_touched FROM cur WHERE cur_id={$rid}";
201 $res = wfQuery( $sql, DB_READ, $fname );
202
203 if ( 0 != wfNumRows( $res ) ) {
204 $this->mRedirectedFrom = $this->mTitle->getPrefixedText();
205 $this->mTitle = $rt;
206 $s = wfFetchObject( $res );
207 }
208 }
209 }
210 }
211 }
212
213 $this->mContent = $s->cur_text;
214 $this->mUser = $s->cur_user;
215 $this->mCounter = $s->cur_counter;
216 $this->mTimestamp = $s->cur_timestamp;
217 $this->mTouched = $s->cur_touched;
218 $this->mTitle->mRestrictions = explode( ",", trim( $s->cur_restrictions ) );
219 $this->mTitle->mRestrictionsLoaded = true;
220 wfFreeResult( $res );
221 } else { # oldid set, retrieve historical version
222 $sql = "SELECT old_text,old_timestamp,old_user,old_flags FROM old " .
223 "WHERE old_id={$oldid}";
224 $res = wfQuery( $sql, DB_READ, $fname );
225 if ( 0 == wfNumRows( $res ) ) { return; }
226
227 $s = wfFetchObject( $res );
228 $this->mContent = Article::getRevisionText( $s );
229 $this->mUser = $s->old_user;
230 $this->mCounter = 0;
231 $this->mTimestamp = $s->old_timestamp;
232 wfFreeResult( $res );
233 }
234 $this->mContentLoaded = true;
235 return $this->mContent;
236 }
237
238 # Gets the article text without using so many damn globals
239 # Returns false on error
240 function getContentWithoutUsingSoManyDamnGlobals( $oldid = 0, $noredir = false ) {
241 global $wgMwRedir;
242
243 if ( $this->mContentLoaded ) {
244 return $this->mContent;
245 }
246 $this->mContent = false;
247
248 $fname = "Article::loadContent";
249
250 if ( ! $oldid ) { # Retrieve current version
251 $id = $this->getID();
252 if ( 0 == $id ) {
253 return false;
254 }
255
256 $sql = "SELECT " .
257 "cur_text,cur_timestamp,cur_user,cur_counter,cur_restrictions,cur_touched " .
258 "FROM cur WHERE cur_id={$id}";
259 $res = wfQuery( $sql, DB_READ, $fname );
260 if ( 0 == wfNumRows( $res ) ) {
261 return false;
262 }
263
264 $s = wfFetchObject( $res );
265 # If we got a redirect, follow it (unless we've been told
266 # not to by either the function parameter or the query
267 if ( !$noredir && $wgMwRedir->matchStart( $s->cur_text ) ) {
268 if ( preg_match( "/\\[\\[([^\\]\\|]+)[\\]\\|]/",
269 $s->cur_text, $m ) ) {
270 $rt = Title::newFromText( $m[1] );
271 if( $rt && $rt->getInterwiki() == "" && $rt->getNamespace() != Namespace::getSpecial() ) {
272 $rid = $rt->getArticleID();
273 if ( 0 != $rid ) {
274 $sql = "SELECT cur_text,cur_timestamp,cur_user," .
275 "cur_counter,cur_restrictions,cur_touched FROM cur WHERE cur_id={$rid}";
276 $res = wfQuery( $sql, DB_READ, $fname );
277
278 if ( 0 != wfNumRows( $res ) ) {
279 $this->mRedirectedFrom = $this->mTitle->getPrefixedText();
280 $this->mTitle = $rt;
281 $s = wfFetchObject( $res );
282 }
283 }
284 }
285 }
286 }
287
288 $this->mContent = $s->cur_text;
289 $this->mUser = $s->cur_user;
290 $this->mCounter = $s->cur_counter;
291 $this->mTimestamp = $s->cur_timestamp;
292 $this->mTouched = $s->cur_touched;
293 $this->mTitle->mRestrictions = explode( ",", trim( $s->cur_restrictions ) );
294 $this->mTitle->mRestrictionsLoaded = true;
295 wfFreeResult( $res );
296 } else { # oldid set, retrieve historical version
297 $sql = "SELECT old_text,old_timestamp,old_user,old_flags FROM old " .
298 "WHERE old_id={$oldid}";
299 $res = wfQuery( $sql, DB_READ, $fname );
300 if ( 0 == wfNumRows( $res ) ) {
301 return false;
302 }
303
304 $s = wfFetchObject( $res );
305 $this->mContent = Article::getRevisionText( $s );
306 $this->mUser = $s->old_user;
307 $this->mCounter = 0;
308 $this->mTimestamp = $s->old_timestamp;
309 wfFreeResult( $res );
310 }
311 $this->mContentLoaded = true;
312 return $this->mContent;
313 }
314
315 function getID() {
316 if( $this->mTitle ) {
317 return $this->mTitle->getArticleID();
318 } else {
319 return 0;
320 }
321 }
322
323 function getCount()
324 {
325 if ( -1 == $this->mCounter ) {
326 $id = $this->getID();
327 $this->mCounter = wfGetSQL( "cur", "cur_counter", "cur_id={$id}" );
328 }
329 return $this->mCounter;
330 }
331
332 # Would the given text make this article a "good" article (i.e.,
333 # suitable for including in the article count)?
334
335 function isCountable( $text )
336 {
337 global $wgUseCommaCount, $wgMwRedir;
338
339 if ( 0 != $this->mTitle->getNamespace() ) { return 0; }
340 if ( $wgMwRedir->matchStart( $text ) ) { return 0; }
341 $token = ($wgUseCommaCount ? "," : "[[" );
342 if ( false === strstr( $text, $token ) ) { return 0; }
343 return 1;
344 }
345
346 # Loads everything from cur except cur_text
347 # This isn't necessary for all uses, so it's only done if needed.
348
349 /* private */ function loadLastEdit()
350 {
351 global $wgOut;
352 if ( -1 != $this->mUser ) return;
353
354 $sql = "SELECT cur_user,cur_user_text,cur_timestamp," .
355 "cur_comment,cur_minor_edit FROM cur WHERE " .
356 "cur_id=" . $this->getID();
357 $res = wfQuery( $sql, DB_READ, "Article::loadLastEdit" );
358
359 if ( wfNumRows( $res ) > 0 ) {
360 $s = wfFetchObject( $res );
361 $this->mUser = $s->cur_user;
362 $this->mUserText = $s->cur_user_text;
363 $this->mTimestamp = $s->cur_timestamp;
364 $this->mComment = $s->cur_comment;
365 $this->mMinorEdit = $s->cur_minor_edit;
366 }
367 }
368
369 function getTimestamp()
370 {
371 $this->loadLastEdit();
372 return $this->mTimestamp;
373 }
374
375 function getUser()
376 {
377 $this->loadLastEdit();
378 return $this->mUser;
379 }
380
381 function getUserText()
382 {
383 $this->loadLastEdit();
384 return $this->mUserText;
385 }
386
387 function getComment()
388 {
389 $this->loadLastEdit();
390 return $this->mComment;
391 }
392
393 function getMinorEdit()
394 {
395 $this->loadLastEdit();
396 return $this->mMinorEdit;
397 }
398
399 function getContributors($limit = 0, $offset = 0)
400 {
401 $fname = "Article::getContributors";
402
403 # XXX: this is expensive; cache this info somewhere.
404
405 $title = $this->mTitle;
406
407 $contribs = array();
408
409 $sql = "SELECT old.old_user, old.old_user_text, " .
410 " user.user_real_name, MAX(old.old_timestamp) as timestamp" .
411 " FROM old, user " .
412 " WHERE old.old_user = user.user_id " .
413 " AND old.old_namespace = " . $title->getNamespace() .
414 " AND old.old_title = '" . $title->getDBkey() . "'" .
415 " AND old.old_user != 0 " .
416 " AND old.old_user != " . $this->getUser() .
417 " GROUP BY old.old_user " .
418 " ORDER BY timestamp DESC ";
419
420 if ($limit > 0) {
421 $sql .= " LIMIT $limit";
422 }
423
424 $res = wfQuery($sql, DB_READ, $fname);
425
426 while ( $line = wfFetchObject( $res ) ) {
427 $contribs[$line->old_user] =
428 array($line->old_user_text, $line->user_real_name);
429 }
430
431 # Count anonymous users
432
433 $res = wfQuery("SELECT COUNT(*) AS cnt " .
434 " FROM old " .
435 " WHERE old_namespace = " . $title->getNamespace() .
436 " AND old_title = '" . $title->getDBkey() . "'" .
437 " AND old_user = 0 ", DB_READ, $fname);
438
439 while ( $line = wfFetchObject( $res ) ) {
440 $contribs[0] = array($line->cnt, 'Anonymous');
441 }
442
443 return $contribs;
444 }
445
446 # This is the default action of the script: just view the page of
447 # the given title.
448
449 function view()
450 {
451 global $wgUser, $wgOut, $wgLang, $wgRequest;
452 global $wgLinkCache, $IP, $wgEnableParserCache;
453
454 $fname = "Article::view";
455 wfProfileIn( $fname );
456
457 # Get variables from query string :P
458 $oldid = $wgRequest->getVal( 'oldid' );
459 $diff = $wgRequest->getVal( 'diff' );
460
461 $wgOut->setArticleFlag( true );
462 $wgOut->setRobotpolicy( "index,follow" );
463
464 # If we got diff and oldid in the query, we want to see a
465 # diff page instead of the article.
466
467 if ( !is_null( $diff ) ) {
468 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
469 $de = new DifferenceEngine( intval($oldid), intval($diff) );
470 $de->showDiffPage();
471 wfProfileOut( $fname );
472 return;
473 }
474
475 if ( !is_null( $oldid ) and $this->checkTouched() ) {
476 if( $wgOut->checkLastModified( $this->mTouched ) ){
477 return;
478 } else if ( $this->tryFileCache() ) {
479 # tell wgOut that output is taken care of
480 $wgOut->disable();
481 $this->viewUpdates();
482 return;
483 }
484 }
485
486 $text = $this->getContent(); # May change mTitle
487 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
488
489 # We're looking at an old revision
490
491 if ( !empty( $oldid ) ) {
492 $this->setOldSubtitle();
493 $wgOut->setRobotpolicy( "noindex,follow" );
494 }
495 if ( "" != $this->mRedirectedFrom ) {
496 $sk = $wgUser->getSkin();
497 $redir = $sk->makeKnownLink( $this->mRedirectedFrom, "",
498 "redirect=no" );
499 $s = wfMsg( "redirectedfrom", $redir );
500 $wgOut->setSubtitle( $s );
501 }
502
503 $wgLinkCache->preFill( $this->mTitle );
504
505 if( $wgEnableParserCache && intval($wgUser->getOption( "stubthreshold" )) == 0 ){
506 $wgOut->addWikiText( $text, true, $this );
507 } else {
508 $wgOut->addWikiText( $text );
509 }
510
511 # Add link titles as META keywords
512 $wgOut->addMetaTags() ;
513
514 $this->viewUpdates();
515 wfProfileOut( $fname );
516 }
517
518 # Theoretically we could defer these whole insert and update
519 # functions for after display, but that's taking a big leap
520 # of faith, and we want to be able to report database
521 # errors at some point.
522
523 /* private */ function insertNewArticle( $text, $summary, $isminor, $watchthis )
524 {
525 global $wgOut, $wgUser, $wgLinkCache, $wgMwRedir;
526 global $wgUseSquid, $wgDeferredUpdateList, $wgInternalServer;
527
528 $fname = "Article::insertNewArticle";
529
530 $this->mCountAdjustment = $this->isCountable( $text );
531
532 $ns = $this->mTitle->getNamespace();
533 $ttl = $this->mTitle->getDBkey();
534 $text = $this->preSaveTransform( $text );
535 if ( $wgMwRedir->matchStart( $text ) ) { $redir = 1; }
536 else { $redir = 0; }
537
538 $now = wfTimestampNow();
539 $won = wfInvertTimestamp( $now );
540 wfSeedRandom();
541 $rand = number_format( mt_rand() / mt_getrandmax(), 12, ".", "" );
542 $isminor = ( $isminor && $wgUser->getID() ) ? 1 : 0;
543 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
544 "cur_comment,cur_user,cur_timestamp,cur_minor_edit,cur_counter," .
545 "cur_restrictions,cur_user_text,cur_is_redirect," .
546 "cur_is_new,cur_random,cur_touched,inverse_timestamp) VALUES ({$ns},'" . wfStrencode( $ttl ) . "', '" .
547 wfStrencode( $text ) . "', '" .
548 wfStrencode( $summary ) . "', '" .
549 $wgUser->getID() . "', '{$now}', " .
550 $isminor . ", 0, '', '" .
551 wfStrencode( $wgUser->getName() ) . "', $redir, 1, $rand, '{$now}', '{$won}')";
552 $res = wfQuery( $sql, DB_WRITE, $fname );
553
554 $newid = wfInsertId();
555 $this->mTitle->resetArticleID( $newid );
556
557 Article::onArticleCreate( $this->mTitle );
558 RecentChange::notifyNew( $now, $this->mTitle, $isminor, $wgUser, $summary );
559
560 if ($watchthis) {
561 if(!$this->mTitle->userIsWatching()) $this->watch();
562 } else {
563 if ( $this->mTitle->userIsWatching() ) {
564 $this->unwatch();
565 }
566 }
567
568 # The talk page isn't in the regular link tables, so we need to update manually:
569 $talkns = $ns ^ 1; # talk -> normal; normal -> talk
570 $sql = "UPDATE cur set cur_touched='$now' WHERE cur_namespace=$talkns AND cur_title='" . wfStrencode( $ttl ) . "'";
571 wfQuery( $sql, DB_WRITE );
572
573 # standard deferred updates
574 $this->editUpdates( $text );
575
576 $this->showArticle( $text, wfMsg( "newarticle" ) );
577 }
578
579
580 /* Side effects: loads last edit */
581 function getTextOfLastEditWithSectionReplacedOrAdded($section, $text, $summary = ""){
582 $this->loadLastEdit();
583 $oldtext = $this->getContent();
584 if ($section != "") {
585 if($section=="new") {
586 if($summary) $subject="== {$summary} ==\n\n";
587 $text=$oldtext."\n\n".$subject.$text;
588 } else {
589
590 # strip NOWIKI etc. to avoid confusion (true-parameter causes HTML
591 # comments to be stripped as well)
592 $striparray=array();
593 $parser=new Parser();
594 $parser->mOutputType=OT_WIKI;
595 $oldtext=$parser->strip($oldtext, $striparray, true);
596
597 # now that we can be sure that no pseudo-sections are in the source,
598 # split it up
599 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
600 $oldtext,-1,PREG_SPLIT_DELIM_CAPTURE);
601 $secs[$section*2]=$text."\n\n"; // replace with edited
602 if($section) { $secs[$section*2-1]=""; } // erase old headline
603 $text=join("",$secs);
604
605 # reinsert the stuff that we stripped out earlier
606 $text=$parser->unstrip($text,$striparray,true);
607 }
608 }
609 return $text;
610 }
611
612 function updateArticle( $text, $summary, $minor, $watchthis, $forceBot = false )
613 {
614 global $wgOut, $wgUser, $wgLinkCache;
615 global $wgDBtransactions, $wgMwRedir;
616 global $wgUseSquid, $wgInternalServer;
617 $fname = "Article::updateArticle";
618
619 if ( $this->mMinorEdit ) { $me1 = 1; } else { $me1 = 0; }
620 if ( $minor && $wgUser->getID() ) { $me2 = 1; } else { $me2 = 0; }
621 if ( preg_match( "/^((" . $wgMwRedir->getBaseRegex() . ")[^\\n]+)/i", $text, $m ) ) {
622 $redir = 1;
623 $text = $m[1] . "\n"; # Remove all content but redirect
624 }
625 else { $redir = 0; }
626
627 $text = $this->preSaveTransform( $text );
628
629 # Update article, but only if changed.
630
631 if( $wgDBtransactions ) {
632 $sql = "BEGIN";
633 wfQuery( $sql, DB_WRITE );
634 }
635 $oldtext = $this->getContent( true );
636
637 if ( 0 != strcmp( $text, $oldtext ) ) {
638 $this->mCountAdjustment = $this->isCountable( $text )
639 - $this->isCountable( $oldtext );
640
641 $now = wfTimestampNow();
642 $won = wfInvertTimestamp( $now );
643 $sql = "UPDATE cur SET cur_text='" . wfStrencode( $text ) .
644 "',cur_comment='" . wfStrencode( $summary ) .
645 "',cur_minor_edit={$me2}, cur_user=" . $wgUser->getID() .
646 ",cur_timestamp='{$now}',cur_user_text='" .
647 wfStrencode( $wgUser->getName() ) .
648 "',cur_is_redirect={$redir}, cur_is_new=0, cur_touched='{$now}', inverse_timestamp='{$won}' " .
649 "WHERE cur_id=" . $this->getID() .
650 " AND cur_timestamp='" . $this->getTimestamp() . "'";
651 $res = wfQuery( $sql, DB_WRITE, $fname );
652
653 if( wfAffectedRows() == 0 ) {
654 /* Belated edit conflict! Run away!! */
655 return false;
656 }
657
658 # This overwrites $oldtext if revision compression is on
659 $flags = Article::compressRevisionText( $oldtext );
660
661 $sql = "INSERT INTO old (old_namespace,old_title,old_text," .
662 "old_comment,old_user,old_user_text,old_timestamp," .
663 "old_minor_edit,inverse_timestamp,old_flags) VALUES (" .
664 $this->mTitle->getNamespace() . ", '" .
665 wfStrencode( $this->mTitle->getDBkey() ) . "', '" .
666 wfStrencode( $oldtext ) . "', '" .
667 wfStrencode( $this->getComment() ) . "', " .
668 $this->getUser() . ", '" .
669 wfStrencode( $this->getUserText() ) . "', '" .
670 $this->getTimestamp() . "', " . $me1 . ", '" .
671 wfInvertTimestamp( $this->getTimestamp() ) . "','$flags')";
672 $res = wfQuery( $sql, DB_WRITE, $fname );
673 $oldid = wfInsertID( $res );
674
675 $bot = (int)($wgUser->isBot() || $forceBot);
676 RecentChange::notifyEdit( $now, $this->mTitle, $me2, $wgUser, $summary,
677 $oldid, $this->getTimestamp(), $bot );
678 Article::onArticleEdit( $this->mTitle );
679 }
680
681 if( $wgDBtransactions ) {
682 $sql = "COMMIT";
683 wfQuery( $sql, DB_WRITE );
684 }
685
686 if ($watchthis) {
687 if (!$this->mTitle->userIsWatching()) $this->watch();
688 } else {
689 if ( $this->mTitle->userIsWatching() ) {
690 $this->unwatch();
691 }
692 }
693 # standard deferred updates
694 $this->editUpdates( $text );
695
696
697 $urls = array();
698 # Template namespace
699 # Purge all articles linking here
700 if ( $this->mTitle->getNamespace() == NS_TEMPLATE) {
701 $titles = $this->mTitle->getLinksTo();
702 Title::touchArray( $titles );
703 if ( $wgUseSquid ) {
704 foreach ( $titles as $title ) {
705 $urls[] = $title->getInternalURL();
706 }
707 }
708 }
709
710 # Squid updates
711 if ( $wgUseSquid ) {
712 $urls = array_merge( $urls, $this->mTitle->getSquidURLs() );
713 $u = new SquidUpdate( $urls );
714 $u->doUpdate();
715 }
716
717 $this->showArticle( $text, wfMsg( "updated" ) );
718 return true;
719 }
720
721 # After we've either updated or inserted the article, update
722 # the link tables and redirect to the new page.
723
724 function showArticle( $text, $subtitle )
725 {
726 global $wgOut, $wgUser, $wgLinkCache;
727 global $wgMwRedir;
728
729 $wgLinkCache = new LinkCache();
730
731 # Get old version of link table to allow incremental link updates
732 $wgLinkCache->preFill( $this->mTitle );
733 $wgLinkCache->clear();
734
735 # Now update the link cache by parsing the text
736 $wgOut = new OutputPage();
737 $wgOut->addWikiText( $text );
738
739 if( $wgMwRedir->matchStart( $text ) )
740 $r = "redirect=no";
741 else
742 $r = "";
743 $wgOut->redirect( $this->mTitle->getFullURL( $r ) );
744 }
745
746 # Add this page to my watchlist
747
748 function watch( $add = true )
749 {
750 global $wgUser, $wgOut, $wgLang;
751 global $wgDeferredUpdateList;
752
753 if ( 0 == $wgUser->getID() ) {
754 $wgOut->errorpage( "watchnologin", "watchnologintext" );
755 return;
756 }
757 if ( wfReadOnly() ) {
758 $wgOut->readOnlyPage();
759 return;
760 }
761 if( $add )
762 $wgUser->addWatch( $this->mTitle );
763 else
764 $wgUser->removeWatch( $this->mTitle );
765
766 $wgOut->setPagetitle( wfMsg( $add ? "addedwatch" : "removedwatch" ) );
767 $wgOut->setRobotpolicy( "noindex,follow" );
768
769 $sk = $wgUser->getSkin() ;
770 $link = $this->mTitle->getPrefixedText();
771
772 if($add)
773 $text = wfMsg( "addedwatchtext", $link );
774 else
775 $text = wfMsg( "removedwatchtext", $link );
776 $wgOut->addWikiText( $text );
777
778 $up = new UserUpdate();
779 array_push( $wgDeferredUpdateList, $up );
780
781 $wgOut->returnToMain( false );
782 }
783
784 function unwatch()
785 {
786 $this->watch( false );
787 }
788
789 function protect( $limit = "sysop" )
790 {
791 global $wgUser, $wgOut, $wgRequest;
792
793 if ( ! $wgUser->isSysop() ) {
794 $wgOut->sysopRequired();
795 return;
796 }
797 if ( wfReadOnly() ) {
798 $wgOut->readOnlyPage();
799 return;
800 }
801 $id = $this->mTitle->getArticleID();
802 if ( 0 == $id ) {
803 $wgOut->fatalError( wfMsg( "badarticleerror" ) );
804 return;
805 }
806
807 $confirm = $wgRequest->getBool( 'wpConfirmProtect' ) && $wgRequest->wasPosted();
808 $reason = $wgRequest->getText( 'wpReasonProtect' );
809
810 if ( $confirm ) {
811
812 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
813 "cur_restrictions='{$limit}' WHERE cur_id={$id}";
814 wfQuery( $sql, DB_WRITE, "Article::protect" );
815
816 $log = new LogPage( wfMsg( "protectlogpage" ), wfMsg( "protectlogtext" ) );
817 if ( $limit === "" ) {
818 $log->addEntry( wfMsg( "unprotectedarticle", $this->mTitle->getPrefixedText() ), $reason );
819 } else {
820 $log->addEntry( wfMsg( "protectedarticle", $this->mTitle->getPrefixedText() ), $reason );
821 }
822 $wgOut->redirect( $this->mTitle->getFullURL() );
823 return;
824 } else {
825 $reason = htmlspecialchars( wfMsg( "protectreason" ) );
826 return $this->confirmProtect( "", $reason, $limit );
827 }
828 }
829
830 # Output protection confirmation dialog
831 function confirmProtect( $par, $reason, $limit = "sysop" )
832 {
833 global $wgOut;
834
835 wfDebug( "Article::confirmProtect\n" );
836
837 $sub = htmlspecialchars( $this->mTitle->getPrefixedText() );
838 $wgOut->setRobotpolicy( "noindex,nofollow" );
839
840 $check = "";
841 $protcom = "";
842
843 if ( $limit === "" ) {
844 $wgOut->setSubtitle( wfMsg( "unprotectsub", $sub ) );
845 $wgOut->addWikiText( wfMsg( "confirmunprotecttext" ) );
846 $check = htmlspecialchars( wfMsg( "confirmunprotect" ) );
847 $protcom = htmlspecialchars( wfMsg( "unprotectcomment" ) );
848 $formaction = $this->mTitle->escapeLocalURL( "action=unprotect" . $par );
849 } else {
850 $wgOut->setSubtitle( wfMsg( "protectsub", $sub ) );
851 $wgOut->addWikiText( wfMsg( "confirmprotecttext" ) );
852 $check = htmlspecialchars( wfMsg( "confirmprotect" ) );
853 $protcom = htmlspecialchars( wfMsg( "protectcomment" ) );
854 $formaction = $this->mTitle->escapeLocalURL( "action=protect" . $par );
855 }
856
857 $confirm = htmlspecialchars( wfMsg( "confirm" ) );
858
859 $wgOut->addHTML( "
860 <form id='protectconfirm' method='post' action=\"{$formaction}\">
861 <table border='0'>
862 <tr>
863 <td align='right'>
864 <label for='wpReasonProtect'>{$protcom}:</label>
865 </td>
866 <td align='left'>
867 <input type='text' size='60' name='wpReasonProtect' id='wpReasonProtect' value=\"" . htmlspecialchars( $reason ) . "\" />
868 </td>
869 </tr>
870 <tr>
871 <td>&nbsp;</td>
872 </tr>
873 <tr>
874 <td align='right'>
875 <input type='checkbox' name='wpConfirmProtect' value='1' id='wpConfirmProtect' />
876 </td>
877 <td>
878 <label for='wpConfirmProtect'>{$check}</label>
879 </td>
880 </tr>
881 <tr>
882 <td>&nbsp;</td>
883 <td>
884 <input type='submit' name='wpConfirmProtectB' value=\"{$confirm}\" />
885 </td>
886 </tr>
887 </table>
888 </form>\n" );
889
890 $wgOut->returnToMain( false );
891 }
892
893 function unprotect()
894 {
895 return $this->protect( "" );
896 }
897
898 # UI entry point for page deletion
899 function delete()
900 {
901 global $wgUser, $wgOut, $wgMessageCache, $wgRequest;
902 $fname = "Article::delete";
903 $confirm = $wgRequest->getBool( 'wpConfirm' ) && $wgRequest->wasPosted();
904 $reason = $wgRequest->getText( 'wpReason' );
905
906 # This code desperately needs to be totally rewritten
907
908 # Check permissions
909 if ( ( ! $wgUser->isSysop() ) ) {
910 $wgOut->sysopRequired();
911 return;
912 }
913 if ( wfReadOnly() ) {
914 $wgOut->readOnlyPage();
915 return;
916 }
917
918 # Better double-check that it hasn't been deleted yet!
919 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
920 if ( ( "" == trim( $this->mTitle->getText() ) )
921 or ( $this->mTitle->getArticleId() == 0 ) ) {
922 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
923 return;
924 }
925
926 if ( $confirm ) {
927 $this->doDelete( $reason );
928 return;
929 }
930
931 # determine whether this page has earlier revisions
932 # and insert a warning if it does
933 # we select the text because it might be useful below
934 $ns = $this->mTitle->getNamespace();
935 $title = $this->mTitle->getDBkey();
936 $etitle = wfStrencode( $title );
937 $sql = "SELECT old_text,old_flags FROM old WHERE old_namespace=$ns and old_title='$etitle' ORDER BY inverse_timestamp LIMIT 1";
938 $res = wfQuery( $sql, DB_READ, $fname );
939 if( ($old=wfFetchObject($res)) && !$confirm ) {
940 $skin=$wgUser->getSkin();
941 $wgOut->addHTML("<b>".wfMsg("historywarning"));
942 $wgOut->addHTML( $skin->historyLink() ."</b>");
943 }
944
945 $sql="SELECT cur_text FROM cur WHERE cur_namespace=$ns and cur_title='$etitle'";
946 $res=wfQuery($sql, DB_READ, $fname);
947 if( ($s=wfFetchObject($res))) {
948
949 # if this is a mini-text, we can paste part of it into the deletion reason
950
951 #if this is empty, an earlier revision may contain "useful" text
952 $blanked = false;
953 if($s->cur_text!="") {
954 $text=$s->cur_text;
955 } else {
956 if($old) {
957 $text = Article::getRevisionText( $old );
958 $blanked = true;
959 }
960
961 }
962
963 $length=strlen($text);
964
965 # this should not happen, since it is not possible to store an empty, new
966 # page. Let's insert a standard text in case it does, though
967 if($length == 0 && $reason === "") {
968 $reason = wfMsg("exblank");
969 }
970
971 if($length < 500 && $reason === "") {
972
973 # comment field=255, let's grep the first 150 to have some user
974 # space left
975 $text=substr($text,0,150);
976 # let's strip out newlines and HTML tags
977 $text=preg_replace("/\"/","'",$text);
978 $text=preg_replace("/\</","&lt;",$text);
979 $text=preg_replace("/\>/","&gt;",$text);
980 $text=preg_replace("/[\n\r]/","",$text);
981 if(!$blanked) {
982 $reason=wfMsg("excontent"). " '".$text;
983 } else {
984 $reason=wfMsg("exbeforeblank") . " '".$text;
985 }
986 if($length>150) { $reason .= "..."; } # we've only pasted part of the text
987 $reason.="'";
988 }
989 }
990
991 return $this->confirmDelete( "", $reason );
992 }
993
994 # Output deletion confirmation dialog
995 function confirmDelete( $par, $reason )
996 {
997 global $wgOut;
998
999 wfDebug( "Article::confirmDelete\n" );
1000
1001 $sub = htmlspecialchars( $this->mTitle->getPrefixedText() );
1002 $wgOut->setSubtitle( wfMsg( "deletesub", $sub ) );
1003 $wgOut->setRobotpolicy( "noindex,nofollow" );
1004 $wgOut->addWikiText( wfMsg( "confirmdeletetext" ) );
1005
1006 $formaction = $this->mTitle->escapeLocalURL( "action=delete" . $par );
1007
1008 $confirm = htmlspecialchars( wfMsg( "confirm" ) );
1009 $check = htmlspecialchars( wfMsg( "confirmcheck" ) );
1010 $delcom = htmlspecialchars( wfMsg( "deletecomment" ) );
1011
1012 $wgOut->addHTML( "
1013 <form id='deleteconfirm' method='post' action=\"{$formaction}\">
1014 <table border='0'>
1015 <tr>
1016 <td align='right'>
1017 <label for='wpReason'>{$delcom}:</label>
1018 </td>
1019 <td align='left'>
1020 <input type='text' size='60' name='wpReason' id='wpReason' value=\"" . htmlspecialchars( $reason ) . "\" />
1021 </td>
1022 </tr>
1023 <tr>
1024 <td>&nbsp;</td>
1025 </tr>
1026 <tr>
1027 <td align='right'>
1028 <input type='checkbox' name='wpConfirm' value='1' id='wpConfirm' />
1029 </td>
1030 <td>
1031 <label for='wpConfirm'>{$check}</label>
1032 </td>
1033 </tr>
1034 <tr>
1035 <td>&nbsp;</td>
1036 <td>
1037 <input type='submit' name='wpConfirmB' value=\"{$confirm}\" />
1038 </td>
1039 </tr>
1040 </table>
1041 </form>\n" );
1042
1043 $wgOut->returnToMain( false );
1044 }
1045
1046 # Perform a deletion and output success or failure messages
1047 function doDelete( $reason )
1048 {
1049 global $wgOut, $wgUser, $wgLang;
1050 $fname = "Article::doDelete";
1051 wfDebug( "$fname\n" );
1052
1053 if ( $this->doDeleteArticle( $reason ) ) {
1054 $deleted = $this->mTitle->getPrefixedText();
1055
1056 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
1057 $wgOut->setRobotpolicy( "noindex,nofollow" );
1058
1059 $sk = $wgUser->getSkin();
1060 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
1061 Namespace::getWikipedia() ) .
1062 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
1063
1064 $text = wfMsg( "deletedtext", $deleted, $loglink );
1065
1066 $wgOut->addHTML( "<p>" . $text . "</p>\n" );
1067 $wgOut->returnToMain( false );
1068 } else {
1069 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
1070 }
1071 }
1072
1073 # Back-end article deletion
1074 # Deletes the article with database consistency, writes logs, purges caches
1075 # Returns success
1076 function doDeleteArticle( $reason )
1077 {
1078 global $wgUser, $wgLang;
1079 global $wgUseSquid, $wgDeferredUpdateList, $wgInternalServer;
1080
1081 $fname = "Article::doDeleteArticle";
1082 wfDebug( "$fname\n" );
1083
1084 $ns = $this->mTitle->getNamespace();
1085 $t = wfStrencode( $this->mTitle->getDBkey() );
1086 $id = $this->mTitle->getArticleID();
1087
1088 if ( "" == $t || $id == 0 ) {
1089 return false;
1090 }
1091
1092 $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ) );
1093 array_push( $wgDeferredUpdateList, $u );
1094
1095 $linksTo = $this->mTitle->getLinksTo();
1096
1097 # Squid purging
1098 if ( $wgUseSquid ) {
1099 $urls = array(
1100 $this->mTitle->getInternalURL(),
1101 $this->mTitle->getInternalURL( "history" )
1102 );
1103 foreach ( $linksTo as $linkTo ) {
1104 $urls[] = $linkTo->getInternalURL();
1105 }
1106
1107 $u = new SquidUpdate( $urls );
1108 array_push( $wgDeferredUpdateList, $u );
1109
1110 }
1111
1112 # Client and file cache invalidation
1113 Title::touchArray( $linksTo );
1114
1115 # Move article and history to the "archive" table
1116 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
1117 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
1118 "ar_flags) SELECT cur_namespace,cur_title,cur_text,cur_comment," .
1119 "cur_user,cur_user_text,cur_timestamp,cur_minor_edit,0 FROM cur " .
1120 "WHERE cur_namespace={$ns} AND cur_title='{$t}'";
1121 wfQuery( $sql, DB_WRITE, $fname );
1122
1123 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
1124 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
1125 "ar_flags) SELECT old_namespace,old_title,old_text,old_comment," .
1126 "old_user,old_user_text,old_timestamp,old_minor_edit,old_flags " .
1127 "FROM old WHERE old_namespace={$ns} AND old_title='{$t}'";
1128 wfQuery( $sql, DB_WRITE, $fname );
1129
1130 # Now that it's safely backed up, delete it
1131
1132 $sql = "DELETE FROM cur WHERE cur_namespace={$ns} AND " .
1133 "cur_title='{$t}'";
1134 wfQuery( $sql, DB_WRITE, $fname );
1135
1136 $sql = "DELETE FROM old WHERE old_namespace={$ns} AND " .
1137 "old_title='{$t}'";
1138 wfQuery( $sql, DB_WRITE, $fname );
1139
1140 $sql = "DELETE FROM recentchanges WHERE rc_namespace={$ns} AND " .
1141 "rc_title='{$t}'";
1142 wfQuery( $sql, DB_WRITE, $fname );
1143
1144 # Finally, clean up the link tables
1145 $t = wfStrencode( $this->mTitle->getPrefixedDBkey() );
1146
1147 Article::onArticleDelete( $this->mTitle );
1148
1149 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
1150 $first = true;
1151
1152 foreach ( $linksTo as $titleObj ) {
1153 if ( ! $first ) { $sql .= ","; }
1154 $first = false;
1155 # Get article ID. Efficient because it was loaded into the cache by getLinksTo().
1156 $linkID = $titleObj->getArticleID();
1157 $sql .= "({$linkID},'{$t}')";
1158 }
1159 if ( ! $first ) {
1160 wfQuery( $sql, DB_WRITE, $fname );
1161 }
1162
1163 $sql = "DELETE FROM links WHERE l_to={$id}";
1164 wfQuery( $sql, DB_WRITE, $fname );
1165
1166 $sql = "DELETE FROM links WHERE l_from={$id}";
1167 wfQuery( $sql, DB_WRITE, $fname );
1168
1169 $sql = "DELETE FROM imagelinks WHERE il_from={$id}";
1170 wfQuery( $sql, DB_WRITE, $fname );
1171
1172 $sql = "DELETE FROM brokenlinks WHERE bl_from={$id}";
1173 wfQuery( $sql, DB_WRITE, $fname );
1174
1175 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
1176 $art = $this->mTitle->getPrefixedText();
1177 $log->addEntry( wfMsg( "deletedarticle", $art ), $reason );
1178
1179 # Clear the cached article id so the interface doesn't act like we exist
1180 $this->mTitle->resetArticleID( 0 );
1181 $this->mTitle->mArticleID = 0;
1182 return true;
1183 }
1184
1185 function rollback()
1186 {
1187 global $wgUser, $wgLang, $wgOut, $wgRequest;
1188
1189 if ( ! $wgUser->isSysop() ) {
1190 $wgOut->sysopRequired();
1191 return;
1192 }
1193 if ( wfReadOnly() ) {
1194 $wgOut->readOnlyPage( $this->getContent() );
1195 return;
1196 }
1197
1198 # Enhanced rollback, marks edits rc_bot=1
1199 $bot = $wgRequest->getBool( 'bot' );
1200
1201 # Replace all this user's current edits with the next one down
1202 $tt = wfStrencode( $this->mTitle->getDBKey() );
1203 $n = $this->mTitle->getNamespace();
1204
1205 # Get the last editor
1206 $sql = "SELECT cur_id,cur_user,cur_user_text,cur_comment FROM cur WHERE cur_title='{$tt}' AND cur_namespace={$n}";
1207 $res = wfQuery( $sql, DB_READ );
1208 if( ($x = wfNumRows( $res )) != 1 ) {
1209 # Something wrong
1210 $wgOut->addHTML( wfMsg( "notanarticle" ) );
1211 return;
1212 }
1213 $s = wfFetchObject( $res );
1214 $ut = wfStrencode( $s->cur_user_text );
1215 $uid = $s->cur_user;
1216 $pid = $s->cur_id;
1217
1218 $from = str_replace( '_', ' ', $wgRequest->getVal( "from" ) );
1219 if( $from != $s->cur_user_text ) {
1220 $wgOut->setPageTitle(wfmsg("rollbackfailed"));
1221 $wgOut->addWikiText( wfMsg( "alreadyrolled",
1222 htmlspecialchars( $this->mTitle->getPrefixedText()),
1223 htmlspecialchars( $from ),
1224 htmlspecialchars( $s->cur_user_text ) ) );
1225 if($s->cur_comment != "") {
1226 $wgOut->addHTML(
1227 wfMsg("editcomment",
1228 htmlspecialchars( $s->cur_comment ) ) );
1229 }
1230 return;
1231 }
1232
1233 # Get the last edit not by this guy
1234 $sql = "SELECT old_text,old_user,old_user_text,old_timestamp,old_flags
1235 FROM old USE INDEX (name_title_timestamp)
1236 WHERE old_namespace={$n} AND old_title='{$tt}'
1237 AND (old_user <> {$uid} OR old_user_text <> '{$ut}')
1238 ORDER BY inverse_timestamp LIMIT 1";
1239 $res = wfQuery( $sql, DB_READ );
1240 if( wfNumRows( $res ) != 1 ) {
1241 # Something wrong
1242 $wgOut->setPageTitle(wfMsg("rollbackfailed"));
1243 $wgOut->addHTML( wfMsg( "cantrollback" ) );
1244 return;
1245 }
1246 $s = wfFetchObject( $res );
1247
1248 if ( $bot ) {
1249 # Mark all reverted edits as bot
1250 $sql = "UPDATE recentchanges SET rc_bot=1 WHERE
1251 rc_cur_id=$pid AND rc_user=$uid AND rc_timestamp > '{$s->old_timestamp}'";
1252 wfQuery( $sql, DB_WRITE, $fname );
1253 }
1254
1255 # Save it!
1256 $newcomment = wfMsg( "revertpage", $s->old_user_text, $from );
1257 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
1258 $wgOut->setRobotpolicy( "noindex,nofollow" );
1259 $wgOut->addHTML( "<h2>" . $newcomment . "</h2>\n<hr />\n" );
1260 $this->updateArticle( Article::getRevisionText( $s ), $newcomment, 1, $this->mTitle->userIsWatching(), $bot );
1261 Article::onArticleEdit( $this->mTitle );
1262 $wgOut->returnToMain( false );
1263 }
1264
1265
1266 # Do standard deferred updates after page view
1267
1268 /* private */ function viewUpdates()
1269 {
1270 global $wgDeferredUpdateList;
1271 if ( 0 != $this->getID() ) {
1272 global $wgDisableCounters;
1273 if( !$wgDisableCounters ) {
1274 Article::incViewCount( $this->getID() );
1275 $u = new SiteStatsUpdate( 1, 0, 0 );
1276 array_push( $wgDeferredUpdateList, $u );
1277 }
1278 $u = new UserTalkUpdate( 0, $this->mTitle->getNamespace(),
1279 $this->mTitle->getDBkey() );
1280 array_push( $wgDeferredUpdateList, $u );
1281 }
1282 }
1283
1284 # Do standard deferred updates after page edit.
1285 # Every 1000th edit, prune the recent changes table.
1286
1287 /* private */ function editUpdates( $text )
1288 {
1289 global $wgDeferredUpdateList, $wgDBname, $wgMemc;
1290 global $wgMessageCache;
1291
1292 wfSeedRandom();
1293 if ( 0 == mt_rand( 0, 999 ) ) {
1294 $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
1295 $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
1296 wfQuery( $sql, DB_WRITE );
1297 }
1298 $id = $this->getID();
1299 $title = $this->mTitle->getPrefixedDBkey();
1300 $shortTitle = $this->mTitle->getDBkey();
1301
1302 $adj = $this->mCountAdjustment;
1303
1304 if ( 0 != $id ) {
1305 $u = new LinksUpdate( $id, $title );
1306 array_push( $wgDeferredUpdateList, $u );
1307 $u = new SiteStatsUpdate( 0, 1, $adj );
1308 array_push( $wgDeferredUpdateList, $u );
1309 $u = new SearchUpdate( $id, $title, $text );
1310 array_push( $wgDeferredUpdateList, $u );
1311
1312 $u = new UserTalkUpdate( 1, $this->mTitle->getNamespace(), $shortTitle );
1313 array_push( $wgDeferredUpdateList, $u );
1314
1315 if ( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
1316 $wgMessageCache->replace( $shortTitle, $text );
1317 }
1318 }
1319 }
1320
1321 /* private */ function setOldSubtitle()
1322 {
1323 global $wgLang, $wgOut;
1324
1325 $td = $wgLang->timeanddate( $this->mTimestamp, true );
1326 $r = wfMsg( "revisionasof", $td );
1327 $wgOut->setSubtitle( "({$r})" );
1328 }
1329
1330 # This function is called right before saving the wikitext,
1331 # so we can do things like signatures and links-in-context.
1332
1333 function preSaveTransform( $text )
1334 {
1335 global $wgParser, $wgUser;
1336 return $wgParser->preSaveTransform( $text, $this->mTitle, $wgUser, ParserOptions::newFromUser( $wgUser ) );
1337 }
1338
1339 /* Caching functions */
1340
1341 # checkLastModified returns true if it has taken care of all
1342 # output to the client that is necessary for this request.
1343 # (that is, it has sent a cached version of the page)
1344 function tryFileCache() {
1345 static $called = false;
1346 if( $called ) {
1347 wfDebug( " tryFileCache() -- called twice!?\n" );
1348 return;
1349 }
1350 $called = true;
1351 if($this->isFileCacheable()) {
1352 $touched = $this->mTouched;
1353 if( $this->mTitle->getPrefixedDBkey() == wfMsg( "mainpage" ) ) {
1354 # Expire the main page quicker
1355 $expire = wfUnix2Timestamp( time() - 3600 );
1356 $touched = max( $expire, $touched );
1357 }
1358 $cache = new CacheManager( $this->mTitle );
1359 if($cache->isFileCacheGood( $touched )) {
1360 global $wgOut;
1361 wfDebug( " tryFileCache() - about to load\n" );
1362 $cache->loadFromFileCache();
1363 return true;
1364 } else {
1365 wfDebug( " tryFileCache() - starting buffer\n" );
1366 ob_start( array(&$cache, 'saveToFileCache' ) );
1367 }
1368 } else {
1369 wfDebug( " tryFileCache() - not cacheable\n" );
1370 }
1371 }
1372
1373 function isFileCacheable() {
1374 global $wgUser, $wgUseFileCache, $wgShowIPinHeader, $wgRequest;
1375 extract( $wgRequest->getValues( 'action', 'oldid', 'diff', 'redirect', 'printable' ) );
1376
1377 return $wgUseFileCache
1378 and (!$wgShowIPinHeader)
1379 and ($this->getID() != 0)
1380 and ($wgUser->getId() == 0)
1381 and (!$wgUser->getNewtalk())
1382 and ($this->mTitle->getNamespace() != Namespace::getSpecial())
1383 and ($action == "view")
1384 and (!isset($oldid))
1385 and (!isset($diff))
1386 and (!isset($redirect))
1387 and (!isset($printable))
1388 and (!$this->mRedirectedFrom);
1389 }
1390
1391 function checkTouched() {
1392 $id = $this->getID();
1393 $sql = "SELECT cur_touched,cur_is_redirect FROM cur WHERE cur_id=$id";
1394 $res = wfQuery( $sql, DB_READ, "Article::checkTouched" );
1395 if( $s = wfFetchObject( $res ) ) {
1396 $this->mTouched = $s->cur_touched;
1397 return !$s->cur_is_redirect;
1398 } else {
1399 return false;
1400 }
1401 }
1402
1403 /* static */ function incViewCount( $id )
1404 {
1405 $id = intval( $id );
1406 global $wgHitcounterUpdateFreq;
1407
1408 if( $wgHitcounterUpdateFreq <= 1 ){ //
1409 wfQuery("UPDATE cur SET cur_counter = cur_counter + 1 " .
1410 "WHERE cur_id = $id", DB_WRITE);
1411 return;
1412 }
1413
1414 # Not important enough to warrant an error page in case of failure
1415 $oldignore = wfIgnoreSQLErrors( true );
1416
1417 wfQuery("INSERT INTO hitcounter (hc_id) VALUES ({$id})", DB_WRITE);
1418
1419 $checkfreq = intval( $wgHitcounterUpdateFreq/25 + 1 );
1420 if( (rand() % $checkfreq != 0) or (wfLastErrno() != 0) ){
1421 # Most of the time (or on SQL errors), skip row count check
1422 wfIgnoreSQLErrors( $oldignore );
1423 return;
1424 }
1425
1426 $res = wfQuery("SELECT COUNT(*) as n FROM hitcounter", DB_WRITE);
1427 $row = wfFetchObject( $res );
1428 $rown = intval( $row->n );
1429 if( $rown >= $wgHitcounterUpdateFreq ){
1430 wfProfileIn( "Article::incViewCount-collect" );
1431 $old_user_abort = ignore_user_abort( true );
1432
1433 wfQuery("LOCK TABLES hitcounter WRITE", DB_WRITE);
1434 wfQuery("CREATE TEMPORARY TABLE acchits TYPE=HEAP ".
1435 "SELECT hc_id,COUNT(*) AS hc_n FROM hitcounter ".
1436 "GROUP BY hc_id", DB_WRITE);
1437 wfQuery("DELETE FROM hitcounter", DB_WRITE);
1438 wfQuery("UNLOCK TABLES", DB_WRITE);
1439 wfQuery("UPDATE cur,acchits SET cur_counter=cur_counter + hc_n ".
1440 "WHERE cur_id = hc_id", DB_WRITE);
1441 wfQuery("DROP TABLE acchits", DB_WRITE);
1442
1443 ignore_user_abort( $old_user_abort );
1444 wfProfileOut( "Article::incViewCount-collect" );
1445 }
1446 wfIgnoreSQLErrors( $oldignore );
1447 }
1448
1449 # The onArticle*() functions are supposed to be a kind of hooks
1450 # which should be called whenever any of the specified actions
1451 # are done.
1452 #
1453 # This is a good place to put code to clear caches, for instance.
1454
1455 # This is called on page move and undelete, as well as edit
1456 /* static */ function onArticleCreate($title_obj){
1457 global $wgEnablePersistentLC, $wgEnableParserCache, $wgUseSquid, $wgDeferredUpdateList;
1458
1459 $titles = $title_obj->getBrokenLinksTo();
1460
1461 # Purge squid
1462 if ( $wgUseSquid ) {
1463 $urls = $title_obj->getSquidURLs();
1464 foreach ( $titles as $linkTitle ) {
1465 $urls[] = $linkTitle->getInternalURL();
1466 }
1467 $u = new SquidUpdate( $urls );
1468 array_push( $wgDeferredUpdateList, $u );
1469 }
1470
1471 # Clear persistent link cache
1472 if ( $wgEnablePersistentLC ) {
1473 LinkCache::linksccClearBrokenLinksTo( $title_obj->getPrefixedDBkey() );
1474 }
1475
1476 # Clear parser cache (not really used)
1477 if ( $wgEnableParserCache ) {
1478 OutputPage::parsercacheClearBrokenLinksTo( $title_obj->getPrefixedDBkey() );
1479 }
1480 }
1481
1482 /* static */ function onArticleDelete($title_obj){
1483 global $wgEnablePersistentLC, $wgEnableParserCache;
1484 if ( $wgEnablePersistentLC ) {
1485 LinkCache::linksccClearLinksTo( $title_obj->getArticleID() );
1486 }
1487 if ( $wgEnableParserCache ) {
1488 OutputPage::parsercacheClearLinksTo( $title_obj->getArticleID() );
1489 }
1490 }
1491
1492 /* static */ function onArticleEdit($title_obj){
1493 global $wgEnablePersistentLC, $wgEnableParserCache;
1494 if ( $wgEnablePersistentLC ) {
1495 LinkCache::linksccClearPage( $title_obj->getArticleID() );
1496 }
1497 if ( $wgEnableParserCache ) {
1498 OutputPage::parsercacheClearPage( $title_obj->getArticleID(), $title_obj->getNamespace() );
1499 }
1500 }
1501 }
1502
1503 ?>